﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Business.UploadFileInfo;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Member
{
    public partial class CommentDetail : System.Web.UI.Page
    {
        protected string Account { get; set; }

        protected string MemberName { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //todo 设置页面控件信息

                int uid = 0;
                try
                {
                    uid = Int32.Parse(Request["uid"]);
                }
                catch
                {

                } 
                
                fc_imgurl.Config = new UploadConfig() { Thumbnail = new ThumbnailConfig() { Width = 300, Height = 300, Mode = "Cut" } };

                MemberComment item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    item = session.Get<MemberComment>(uid);
                    if (item != null)
                    {
                        //保存uid
                        ViewState["uid"] = uid;
                        MemberName = session.CreateSQLQuery("select member_name from member_info where account_id=:acc")
                            .SetParameter("acc", item.MemberAccount)
                            .UniqueResult<string>();
                    }
                    else
                    {
                        item = new MemberComment();
                        item.TargetCode = Request["TargetCode"];
                        item.TargetKind = Request["TargetKind"]; 
                    }
                }

                //在此设置控件的初始属性
                Account = item.MemberAccount;
                status.SelectedValue = item.Status;
                targetCode.Text = item.TargetCode;
                targetKind.Text = item.TargetKind;
                score.Text = string.Format("{0}",item.Score);
                date.Text = string.Format("{0:yyyy-MM-dd HH:mm:ss}",item.RecordDate);
                content.Text = item.Content;
                fc_imgurl.Value = item.ImageUrl;
            }
            else
            {
                //todo 保存数据

                int uid = ViewState["uid"] == null ? 0 : (int)ViewState["uid"];
                MemberComment item = null;
                using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    item = session.Get<MemberComment>(uid);
                    if (item == null)
                    {
                        item = new MemberComment();
                    }

                    //在此设置item的各个属性
                    item.MemberAccount = Request["member.Account"];
                    item.RecordDate = DateTime.Parse(date.Text);
                    item.TargetCode = targetCode.Text;
                    item.TargetKind = targetKind.Text;
                    item.Status = status.SelectedValue;
                    item.Content = content.Text;
                    item.ImageUrl = fc_imgurl.Value;
                    try
                    {
                        item.Score = Convert.ToInt32(score.Text);
                    }
                    catch (System.Exception ex)
                    {
                    }

                    //保存并更新item
                    session.SaveOrUpdate(item);
                    session.Flush();
                }


                //框架返回成功提示
                ZLCMS.Business.DwzFrame.FrameHelper _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);
                ZLCMS.Business.DwzFrame.ReturnCode rc = new ZLCMS.Business.DwzFrame.ReturnCode()
                {
                    statusCode = 200,
                    message = "操作成功"
                };

                rc.callbackType = "forward";
                rc.forwardUrl = "/SystemManage/Basics/Shop/Member/CommentDetail.aspx?tabid=CommentDetail&TargetCode="
                    + item.TargetCode + "&TargetKind=" + item.TargetKind;

                if (Request["submit_op"] != "1")
                {
                    rc.forwardUrl += "&uid=" + item.Id;
                }

                _framePage.Alert(rc);
            }

        }
    }
}